<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script>
        class Person {
            #age;// 定义私有属性,外部无法直接访问
            name;// 定义公有属性
            constructor(name, age) {
                this.name = name;
                this.#setAge(age);
            }
            // 私有函数，不能在类的外部访问
            #setAge(age) {
                this.#age = age;
            }
            // 自定义方法，通过方法访问私有属性
            getAge() {
                return this.#age;
            }
        }
        let person = new Person('张三', 20);
        console.log(person.name); // 张三
        console.log(person.getAge()); // 20;
        // 报错 SyntaxError: Private field '#setAge' must be declared in an …
        console.log(person.#setAge(18));
        // 报错 SyntaxError: Private field '#age' must be declared in an…
        console.log(person.#age);
    </script>
</head>
<body>
    
</body>
</html>